package yxy.cherry.paycenter;

import com.alibaba.fastjson.parser.ParserConfig;
import org.joda.time.DateTime;
import yxy.apple.database.DAOManager;
import yxy.apple.database.DAOSDKManager;
import yxy.apple.http.server.HttpServer;
import yxy.apple.logger.Logger;
import yxy.apple.util.GetIpAndPort;
import yxy.apple.util.redisson.FastjsonCodec;
import yxy.apple.util.redisson.RedisConnection;
import yxy.banana.launcher.Reactable;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.actdata.DataCenterAct;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.cherry.paycenter.service.OrderService;
import yxy.cherry.paycenter.service.SdkHttpService;
import yxy.cherry.paycenter.service.SdkHttpsService;
import yxy.game.pm2.bean.OrderUrl;
import yxy.game.pm2.bean.area.AreaBean;

import java.util.ArrayList;
import java.util.Collection;

public class Reactor implements Reactable<Configuration> {

	protected static Logger logger = Logger.create(Reactor.class);

	public static final Reactor instance = new Reactor();

	private HttpServer server = null;

	protected Reactor() {

	}

	public void shutdown() {
		if (this.server != null) {
			logger.info("正在关闭HTTP服务...");
			this.server.stop();
		}
	}

	public void start(Configuration configuration) throws Exception {
		ParserConfig.getGlobalInstance().setAutoTypeSupport(true);

		logger.info("正在初始化数据库连接1...");
		DAOManager.init(configuration.DBSDKConfig);
		logger.info("正在初始化数据库连接2...");
		DAOSDKManager.init(configuration.DBSDKConfig);

		logger.info("正在初始化Redis连接...");
		initRedis(configuration);
		
		int orderprot = configuration.OrderProt;
		setOrderUrl(orderprot);
		
		logger.info(DateTime.now() + "启动创建订单http接口");
		OrderService.getInstance().httpinit(orderprot);
		
		SdkHttpService.getInstance().httpinit(configuration);	//http
		if(configuration.SSL.Enabled) {
			SdkHttpsService.getInstance().httpsinit(configuration);	//https			
		}
	}

	private void setOrderUrl(int orderprot) throws Exception{
		
		if(OrderUrl.orderurl() == null) {
//		String url = "http://127.0.0.1:30000/sdk";
			String orderurl = "http://" + GetIpAndPort.getLocalIP() + ":" + orderprot + "/sdk";
			OrderUrl.createorderurl(orderurl);			
		}
	}
	
	private void initRedis(Configuration config) throws Exception {
		{
			RedisConnection redisUser = new RedisConnection();
			redisUser.connect(config.UserRedis.Address, config.UserRedis.Password, config.UserRedis.Database, true, new FastjsonCodec());
			RedisManager.getInstance().addConnection(RedisType.REDIS_USER, redisUser);
		}
		{
			RedisConnection redisGame = new RedisConnection();
			redisGame.connect(config.GameRedis.Address, config.GameRedis.Password, config.GameRedis.Database, true, new FastjsonCodec());
			RedisManager.getInstance().addConnection(RedisType.REDIS_GAME, redisGame);
		}
		{
			RedisConnection redisData = new RedisConnection();
			redisData.connect(config.DataRedis.Address, config.DataRedis.Password, config.DataRedis.Database, true, new FastjsonCodec());
			RedisManager.getInstance().addConnection(RedisType.REDIS_DATA, redisData);

			DataCenter.init(redisData, false);
			initDataCenterAct();
		}

	}
	
	private void initDataCenterAct() {
		Collection<AreaBean> abs = AreaBean.all();
		ArrayList<Integer> areaids = new ArrayList<Integer>();
		for(AreaBean ab : abs) {
			areaids.add(ab.getAreaId());
		}
		
		DataCenterAct.init(RedisManager.getInstance().getConnection(RedisType.REDIS_DATA), false, areaids);
	}

}
